﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives



#endregion

namespace ZO.SmartCore.Standards.Unicode.Normalization
{
    internal static class Composition
    {

        #region Constructors

        #endregion

        #region Destructor

        #endregion

        #region Fields
        private static readonly int[][] Compose_Data = UAX15.Compose_Data;
        private static readonly int[] Compose_Pages = UAX15.Compose_Pages;
        private static readonly int Compose_SingleFirstStart = UAX15.Compose_SingleFirstStart;
        private static readonly int Compose_SingleSecondStart = UAX15.Compose_SingleSecondStart;
        private static readonly char[][] Compose_SingleFirst = UAX15.Compose_SingleFirst;
        private static readonly char[][] Compose_SingleSecond = UAX15.Compose_SingleSecond;
        private static readonly int Compose_MultiSecondStart = UAX15.Compose_MultiSecondStart;
        private static readonly char[][] Compose_MultiFirst = UAX15.Compose_MultiFirst;

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties

        #endregion

        #region Methods
        private static int IndexOf(char c)
        {

            int page;
            int idx = -1;

            if ((c >> 8) < Compose_Pages.Length)
            {
                page = Compose_Pages[c >> 8];
                idx = ((page != -1) ? Compose_Data[page][c & 0xff] : -1);
            } // if

            return idx;
        } // IndexOf

        /// <summary>
        /// Compose two characters canonically.
        /// </summary>
        /// <param name="a">First character.</param>
        /// <param name="b">Second character.</param>
        /// <returns>The composed character or -1 if no composition could be found.</returns>
        public static int Compose(char a, char b)
        {
            int ai, bi;
            int result = -1;

            ai = IndexOf(a);
            bi = IndexOf(b);

            if (result == -1)
            {
                if ((ai >= Compose_SingleFirstStart) && (ai < Compose_SingleSecondStart))
                {
                    if (b == Compose_SingleFirst[ai - Compose_SingleFirstStart][0])
                    {
                        result = Compose_SingleFirst[ai - Compose_SingleFirstStart][1];
                    } // if
                    else
                    {
                        result = -1;
                    } // else
                } // if
            } // if

            if (result == -1)
            {
                bi = IndexOf(b);
                if (bi >= Compose_SingleSecondStart)
                {
                    if (a == Compose_SingleSecond[bi - Compose_SingleSecondStart][0])
                    {
                        result = Compose_SingleSecond[bi - Compose_SingleSecondStart][1];
                    } // if
                    else
                    {
                        result = -1;
                    } // else
                } // if
            } // if

            if (result == -1)
            {
                if ((ai >= 0) &&
                        (ai < Compose_MultiSecondStart) &&
                        (bi >= Compose_MultiSecondStart) &&
                        (bi < Compose_SingleFirstStart))
                {
                    char[] f = Compose_MultiFirst[ai];
                    char r;

                    if (bi - Compose_MultiSecondStart < f.Length)
                    {
                        r = f[bi - Compose_MultiSecondStart];
                        result = ((r == 0) ? -1 : r);
                    } // if
                } // if
            } // if

            return result;
        } // Compose
        #endregion
    }
}
